<template>
  <div class="address-contain">
    <van-nav-bar
      class="page-nav-bar"
      title="我的地址"
      left-arrow
      @click-left="goBack"
    />

    <van-empty v-if="!list.length" description="暂无地址，点击底部添加" />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="$router.push('/address/info')"
      @edit="$router.push('/address/info?id='+$event.id)"
      @select="$emit('close',$event.id)"
    />
  </div>
</template>

<script>
import { getAddressList } from "@/api/address";
export default {
  name:'Address',
  data() {
    return { 
      list: [],
      chosenAddressId:'',
      type:''
    };
  },
  created() {
    this.initData();
  },
  methods: {
    // 返回
    goBack(){
      if(this.type){
        this.$emit('close',this.chosenAddressId)
      }else{
        this.$router.go(-1)
      }
    },
    // 获取地址
    async initData() {
      this.list = await getAddressList();
      this.chosenAddressId = this.list.find(item=>item.defaultStatus === 1).id
      this.list = this.list.map(item=>({
        id:item.id,
        name:item.name,
        tel:item.phoneNumber,
        address:item.province+item.region+item.detailAddress,
        isDefault:item.defaultStatus === 1
      }))
    }
  },
};
</script>

<style lang="less" scoped>
body {
  background-color: #f4f4f4;
}
.address-contain {
  padding-top: 80px;
  padding-bottom: 100px;
  .item1 {
    margin-top: 20px;
    background-color: #fff;
  }
  .item {
    padding: 20px 0px 15px 30px;
    display: flex;
    height: 90px;
    .left {
      padding-top: 10px;
      width: 150px;
      font-size: 30px;
    }
    .right {
      font-size: 30px;
    }
  }
  .btn {
    height: 100%;
    width: 180px;
    font-size: 32px;
  }
  .page-nav-bar {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
  }
  .add {
    position: fixed;
    bottom: 0;
    left: 0;
    border: unset;
    font-size: 30px;
  }
}
.gogo {
  position: relative;
}
.gogo::after {
  content: "默认";
  font-size: 24px;
  display: block;
  position: absolute;
  left: 30px;
  top: 83px;
  padding: 0 10px;
  color: #e11c34;
  border: solid 1px #e11c34;
}
</style>